package com.scau.hwadee.examination.controller;

import com.scau.hwadee.examination.entity.Course;
import com.scau.hwadee.examination.entity.Examfee;
import com.scau.hwadee.examination.service.ExamService;
import com.scau.hwadee.examination.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author YHX
 * @date 2019/7/31 18:32
 * description
 */
@Slf4j
@Controller
@RequestMapping("exam")
@Api(tags = "ExamController",description = "考试管理模块")
public class ExamController {

    @Autowired
    private ExamService examService;

    @GetMapping("allExamSessions")
    @ApiOperation(value = "获取所有的考次信息")
    public ResponseEntity<ResponseVo> getAllExamSessions(@RequestParam("pageNum")int pageNum,
                                                         @RequestParam("pageSize") int pageSize){
        ResponseVo vo=ResponseVo.instance();
        vo.setData(examService.getAllExamSessions(pageNum,pageSize));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("allCourseByExamSessionId")
    @ApiOperation(value = "根据考次信息ID获取所有的课程信息")
    public ResponseEntity<ResponseVo> getAllCoursesByExamSession(@RequestParam("pageNum")int pageNum,
                                                                 @RequestParam("pageSize") int pageSize,
                                                                 @RequestParam("examSessionId") String examSessionId){
        ResponseVo vo=ResponseVo.instance();
        vo.setData(examService.getAllCourseFromExamSessionId(pageNum,pageSize,examSessionId));
        return new ResponseEntity<>(vo,HttpStatus.OK);
    }

    @PostMapping("flatFee")
    @ApiOperation(value = "设置统一费用")
    public ResponseEntity<ResponseVo> setFlatFeeByExamSessionId(@RequestBody Map<String,Object> params){
        String id=params.get("examSessionId").toString();
        BigDecimal money=new BigDecimal(params.get("money").toString());
        List<Examfee> examfees=new ArrayList<>();
        List<Course> courses=examService.getAllCourseFromExamSessionId(1,1000000,id).getList();
        for(Course course:courses){
            Examfee examfee=new Examfee();
            examfee.setEsId(id);
            examfee.setCsId(course.getCsId());
            examfee.setExamfee(money);
            examfees.add(examfee);
        }
        int cnt=examService.insertExamFeeList(examfees);
        String message;
        if (cnt>0){
            message="统一设置费用成功";
        }else {
            message="统一设置费用失败";
        }
        ResponseVo vo=ResponseVo.instance();
        vo.setMessage(message);
        return new ResponseEntity<>(vo,HttpStatus.OK);
    }

    @PostMapping("personalFee")
    @ApiOperation(value = "对课程费用进行个性化设置")
    public ResponseEntity<ResponseVo> setPersonalFee(@RequestBody Map<String,Object> params){
        ResponseVo vo=ResponseVo.instance();
        Examfee examfee=new Examfee();
        String eid=params.get("examSessionId").toString();
        String cid=params.get("courseId").toString();
        BigDecimal money=new BigDecimal(params.get("money").toString());
        examfee.setExamfee(money);
        examfee.setCsId(cid);
        examfee.setEsId(eid);
        int cnt=examService.updateExamFee(examfee);
        String message;
        if (cnt>0){
            message="更新费用成功";
        }else {
            message="更新费用失败";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo,HttpStatus.OK);
    }

}
